package vip.xiaonuo.sys.modular.sec.provider;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sys.api.SysSecApi;
import vip.xiaonuo.sys.modular.sec.entity.SysSec;
import vip.xiaonuo.sys.modular.sec.enums.SysSecEnum;
import vip.xiaonuo.sys.modular.sec.mapper.SysSecMapper;
import vip.xiaonuo.sys.modular.sec.service.SysSecService;
import vip.xiaonuo.sys.modular.user.service.SysUserService;

import javax.annotation.Resource;

@Service
public class SysSecApiProvider implements SysSecApi {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysSecService sysSecService;

    @Resource
    private SysSecMapper sysSecMapper;

    @Override
    public JSONObject getSysSecByLevelWithoutException(Integer level) {
        QueryWrapper<SysSec> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(level)) {
            queryWrapper.lambda().eq(SysSec::getLevel, level);
        }
            queryWrapper.lambda().eq(SysSec::getStatus, SysSecEnum.ENABLE);

        SysSec sysSec = sysSecService.getOne(queryWrapper, false);
         if(ObjectUtil.isNotEmpty(sysSec)) {
            return JSONUtil.parseObj(sysSec);
        }
        return null;
    }

    @Override
    public String getSysSecSubjectNameByLevelWithoutException(Integer level) {
        QueryWrapper<SysSec> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(level)) {
            queryWrapper.lambda().eq(SysSec::getLevel, level);
        }
        queryWrapper.lambda().eq(SysSec::getStatus, SysSecEnum.ENABLE);

        SysSec sysSec = sysSecService.getOne(queryWrapper, false);
        if(ObjectUtil.isNotEmpty(sysSec)) {
            return sysSec.getSubjectName();
        }
        return null;
    }

    @Override
    public String getSysSecObjectNameByLevelWithoutException(Integer level) {
        QueryWrapper<SysSec> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(level)) {
            queryWrapper.lambda().eq(SysSec::getLevel, level);
        }
        queryWrapper.lambda().eq(SysSec::getStatus, SysSecEnum.ENABLE);

        SysSec sysSec = sysSecService.getOne(queryWrapper, false);
        if(ObjectUtil.isNotEmpty(sysSec)) {
            return sysSec.getObjectName();
        }
        return null;
    }

    @Override
    public Integer getLowestSysSec() {
        QueryWrapper<SysSec> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().eq(SysSec::getStatus, SysSecEnum.ENABLE);

        SysSec sysSec = sysSecMapper.selectOne(queryWrapper.orderByAsc("level").last("limit 1"));
        if(ObjectUtil.isNotEmpty(sysSec)) {
            return sysSec.getLevel();
        }
        return null;
    }

    @Override
    public String getSecLevelByUserId(String userId) {
        return sysUserService.getUserById(userId).getSecLevel();
    }
}
